"use strict";

define('WareListView', [
  'jquery', 'underscore', 'backbone', 'api',
  'text!templates/shopAround/wareList.html'
], function ($, _, Backbone, api,
             tpl) {
  return Backbone.View.extend({

    initialize: function () {
      this.template = _.template(tpl);
    },

    events: {
      "click .add_to_cart": "addToCart",
      "click .buy_now": "buyNow"
    },

    addToCart: function (event) {
      console.log('addToCart');
      var target = $(event.currentTarget);
      let wareId = target.data("wareid");
      api.addToCart(wareId, 1, function (err, data) {
        console.log(err, data);
      });
    },

    buyNow: function (event) {
      console.log('buyNow');
      var target = $(event.currentTarget);
      let wareId = target.data("wareid");
      api.addToCart(wareId, 1, function (err, data) {
        console.log(err, data);
        // 跳到支付页面 TODO
      });
    },
    aaa:function(){ console.log('aaa');},

    customerQuery: function (locationId, categoryId, cb) {
      api.customerQuery(locationId, categoryId, function (err, wares) {
        cb(err, wares);
      });
    },

    render: function (cb) {
      var self = this;
      let data = {wares: []};

      api.customerQuery(self.locationId, self.categoryId, function (err, wares) {
        console.log(err, wares);
        data.wares = wares;
        let tmpl = self.template(data);
        $(self.el).html(tmpl);
        cb && cb();
      });
    }
  });
});
